package com.feng.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.feng.cloud.common.annotation.ControllerEndpoint;
import com.feng.cloud.common.response.RestResponse;
import com.feng.cloud.system.dto.DeptTree;
import com.feng.cloud.system.entity.Dept;
import com.feng.cloud.system.service.DeptService;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName DeptController
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/25 10:28
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
public class DeptController implements IDeptController {

    private final DeptService deptService;


    /**
     * 删除部门
     * @param deptIds deptIds
     * @return RestResponse<Void>
     */
    @Override
    @PreAuthorize("hasAuthority('dept:delete')")
    @ControllerEndpoint(operation = "删除部门", exceptionMessage = "删除部门失败")
    public RestResponse<Void> deleteDept(@NotBlank(message = "{required}") String deptIds) {
        deptService.deleteDept(Arrays.asList(deptIds.split(StringPool.COMMA)));
        return new RestResponse<>();
    }

    /**
     * 更新部门
     * @param dept dept
     * @return RestResponse<Dept>
     */
    @Override
    @PreAuthorize("hasAuthority('dept:update')")
    @ControllerEndpoint(operation = "修改部门", exceptionMessage = "修改部门失败")
    public RestResponse<Dept> updateDept(@Valid Dept dept) {
        deptService.updateDept(dept);
        return new RestResponse<Dept>().data(dept);
    }

    /**
     * 增加部门
     * @param dept dept
     * @return RestResponse<Dept>
     */
    @Override
    @PreAuthorize("hasAuthority('dept:add')")
    @ControllerEndpoint(operation = "新增部门", exceptionMessage = "新增部门失败")
    public RestResponse<Dept> addDept(@Valid Dept dept) {
        deptService.addDept(dept);
        return new RestResponse<Dept>().data(dept);
    }

    @Override
    public List<Dept> selectList(Dept dept) {
        return deptService.selectList(dept);
    }

    @Override
    public List<Dept> list() {
        return deptService.list();
    }

    @GetMapping("tree")
    @PreAuthorize("hasAuthority('dept:view')")
    public RestResponse<List<DeptTree>> selectDeptTree(Dept dept) {
        return new RestResponse<List<DeptTree>>().data(deptService.selectDeptTree(dept));
    }

    @PostMapping("excel")
    @PreAuthorize("hasAuthority('dept:export')")
    @ControllerEndpoint(operation = "导出部门数据", exceptionMessage = "导出Excel失败")
    public void export(Dept dept,HttpServletResponse response) {
        List<Dept> depts = deptService.selectList(dept);
        ExcelKit.$Export(Dept.class, response).downXlsx(depts, false);
    }
}
